import 'package:flutter/material.dart';
import 'package:flutter_app_demo/pages/flame_example/flame_demo_list/provider.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';

class FlameDemoListPage extends StatelessWidget {
  const FlameDemoListPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (BuildContext context) {
        final provider = FlameDemoListProvider();
        provider.initData(context); // 传递 context 用于检查 mounted
        return provider;
      },
      builder: (context, child) => _buildPage(context),
    );
  }

  Widget _buildPage(BuildContext context) {
    final provider = context.watch<FlameDemoListProvider>();

    return ListView.builder(
      padding: const EdgeInsets.symmetric(vertical: 10.0),
      itemBuilder: (context, index) {
        return Container(
          margin: const EdgeInsets.only(left: 16.0, right: 16.0, bottom: 16.0),
          padding: const EdgeInsets.all(12.0),
          decoration: BoxDecoration(
            color: const Color(0xFF262626),
            borderRadius: BorderRadius.circular(12.0),
          ),
          child: ListTile(
            contentPadding: EdgeInsets.zero,
            title: Row(
              children: [
                Padding(
                  padding: const EdgeInsets.only(left: 10.0),
                  child: Text(
                    style: const TextStyle(color: Colors.white),
                    provider.flameDemoList[index]['title'],
                  ),
                ),
              ],
            ),
            onTap: () =>
                context.pushNamed(provider.flameDemoList[index]['router']),
          ),
        );
      },
      itemCount: provider.demoListLength,
    );
  }
}
